<template>
  <!-- 我申报的金融产品 -->
  <div class="layout">
    <ContainerTitle text="我的融资" class="title">
      <template>
        <Buttons :buttons="buttons" @goRelease="$router.push({ path: 'myFinancingEdit' })"></Buttons>
      </template>
    </ContainerTitle>
    <div class="msgDiv">
      <el-table
        :data="myFinancingList"
        style="width: 100%"
        min-height="400"
        :border="true"
        :header-cell-style="{ background: '#F4F4F4', color: '#045DB9' }"
      >
        <el-table-column prop="appealName" label="诉求标题" width="150">
        </el-table-column>
        <el-table-column prop="contacts" label="联系人" width="80">
        </el-table-column>
        <el-table-column prop="contactsTel" label="联系电话" width="120">
        </el-table-column>

        <el-table-column prop="type" label="金融类型" width="100">
        </el-table-column>

        <el-table-column label="融资类型" width="100">
          <template slot-scope="scope">
            <span>{{
              $selectDict(FinancialList, scope.row.financingTypeVal)
                .dictLabel
            }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="financingMoney" label="融资金额" width="100">
          <template slot-scope="scope">
            <span>
              {{
                scope.row.financingMoney
                  ? scope.row.financingMoney + "万"
                  : ""
              }}
            </span>
          </template>
        </el-table-column>

        <el-table-column prop="releaseTimeStr" label="发布时间" width="160">
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template slot-scope="scope">
            <span
              :style="
                'color: ' +
                formatApplyStatus(scope.row.financingStatusVal).color
              "
              >{{
                formatApplyStatus(scope.row.financingStatusVal).label
              }}</span
            >
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="160">
          <template slot-scope="scope">
            <span class="rowBtn" @click="toDetail(scope.row)">查看</span>
            <span
              class="rowBtn"
              v-if="
                scope.row.financingStatusVal == 0 ||
                scope.row.financingStatusVal == 3
              "
              @click="toEdit(scope.row)"
              >编辑</span
            >
            <span
              class="rowBtn"
              v-if="
                scope.row.financingStatusVal == 0 ||
                scope.row.financingStatusVal == 3
              "
              @click="Dele(scope.row.companyFinancingId)"
              >删除</span
            >
            <span
              class="rowBtn"
              v-if="scope.row.financingStatusVal == 0"
              @click="release(scope.row.companyFinancingId)"
              >发布</span
            >
            <span
              class="rowBtn"
              v-if="scope.row.financingStatusVal == 1"
              @click="finish(scope.row.companyFinancingId)"
              >完成</span
            >
            <span
              class="rowBtn"
              v-if="scope.row.financingStatusVal == 1"
              @click="closeClick(scope.row.companyFinancingId)"
              >关闭</span
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20, 25, 30]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      class="titleTop"
      title="融资完成"
      :visible.sync="dialogFormVisible"
      center
    >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item
          label="注资企业名称:"
          style="width: 360px"
          :label-width="formLabelWidth"
          prop="investmentCompany"
        >
          <el-input
            v-model.trim="form.investmentCompany"
            autocomplete="off"
            maxlength="40"
            placeholder="请输入注册企业名称"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="注资企业来源:"
          style="width: 360px"
          :label-width="formLabelWidth"
        >
          <div class="circleBoxRow">
            <div
              class="circleBox"
              v-for="(item, index) in radioChoice"
              @click="choice(item.value)"
              :key="index"
            >
              <div class="circle" :class="{ active: item.active }"></div>
              {{ item.text }}
            </div>
          </div>
        </el-form-item>
        <el-form-item
          label="融资金额:"
          style="width: 360px"
          :label-width="formLabelWidth"
          prop="investmentMoney"
        >
          <!-- type为number maxlength没用 -->
          <el-input
          @input="changeInvestmentMoney"
            :value="form.investmentMoney"
            autocomplete="off"
            type="number"
            placeholder="请输入融资金额"
          >
            <div slot="append">万元</div>
          </el-input>
        </el-form-item>
        <el-form-item
          label="融资达成时间:"
          style="width: 360px"
          :label-width="formLabelWidth"
          prop="financingTimeStr"
        >
          <el-date-picker
            v-model.trim="form.financingTimeStr"
            autocomplete="off"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="date"
            placeholder="请选择融资达成时间"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <Buttons :buttons="sendBtn" @cancelDialog="cancelDialog" @getFinish="getFinish"></Buttons>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getFinancingList, subFinancingComplete, subFinancingClose, subFinancingRelease, subFinancingDelete } from "@/common/api/company.js";
export default {
  data() {
    return {
      buttons:[{ text: "新增融资", type: "success", size: "l", method: "goRelease" }],
      sendBtn:[
        { text: "取消", type: "normal", size: "l", method: "cancelDialog" },
        { text: "确定", type: "success", size: "l", method: "getFinish" }
      ],
      name: "finance",
      currentPage: 1,
      pageSize: 10,
      total: 0,
      crumbs: [
        {
          label: "企业空间",
          value: "company",
        },
        {
          label: "金融服务",
          value: "finance",
        },
        {
          label: "我的融资",
          value: "myFinancing",
          choose: true,
        },
      ],
      applyStatusList: [
        { value: 0, label: "待发布", color: "#333333" },
        { value: 1, label: "融资中", color: "#333333" },
        { value: 2, label: "融资成功", color: "#10A217" },
        { value: 3, label: "融资失败", color: "#d00000" },
      ],
      radioChoice: [
        { value: 0, text: "意向企业", active: true },
        { value: 1, text: "其它", active: false },
      ],
      myFinancingList: [],
      FinancialList: [],
      status: [],
      form: {
        id: "",
        investmentCompany: "",
        investmentMoney: "",
        financingTimeStr: "",
        companySource: "0",
      },
      rules: {
        investmentCompany: [
          {
            required: true,
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请输入注资企业名称"));
              } else if (!/^[\u4E00-\u9FA5A-Za-z]{0,}$/.test(value)) {
                callback(new Error("请输入正确的注资企业名称"));
              } else {
                callback();
              }
            },
          },
        ],
        investmentMoney: [
          {
            required: true,
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请输入融资金额"));
              } else if (!/^[0-9.]{1,10}$/.test(value)) {
                callback(new Error("请输入正确的融资金额"));
              } else {
                callback();
              }
            },
          },
        ],
        financingTimeStr: [
          { required: true, message: "请选择融资达成时间", trigger: "blur" },
        ],
      },
      pickerOptions: {
        disabledDate: (time) => time.getTime() > Date.now(),
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
    };
  },

  mounted() {
    this.getDict();
    this.getList();
  },
  methods: {
    // 对投资金额做出限制
    changeInvestmentMoney(e){
      if(e.indexOf(".") != -1 && e.indexOf(".") != e.lastIndexOf(".")){
        return;
      }
      if(e.length <= 10){
        if(e.indexOf(".") != -1 && (e.indexOf(".")+3) < e.length){
          e = e.slice(0,e.indexOf(".")+3)
        }
        this.form.investmentMoney = e
      }
    },
    async getList() {
      let res = await getFinancingList({ pageNum: this.currentPage, pageSize: this.pageSize });
      if (res.code == 200) {
        this.myFinancingList = res.rows;
        this.total = res.total;
      }
    },
    // 字典
    async getDict() {
      // 调用公共方法中的查询字典表
      this.getDictList(this,`finance_financing_type,finance_com_financing_status`).then(res => {
        this.FinancialList = res.finance_financing_type;
        this.status = res.finance_com_financing_status;
      })
    },

    finish(id) {
      this.dialogFormVisible = true;
      this.form.id = id;
    },

    cancelDialog() {
      this.form = {
        investmentCompany: "",
        investmentMoney: "",
        financingTimeStr: "",
        companySource: "0",
      };
      this.dialogFormVisible = false;
    },

    // 完成
    async getFinish() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          let res = await subFinancingComplete({ ...this.form });
          if (res.code == 200) {
            this.dialogFormVisible = false;
            this.getList();
            this.$message({
              type: "success",
              message: "确定完成成功!",
            });
          }
        }
      });
    },

    // 关闭
    async getClose(id) {
      let res = await subFinancingClose({ id: id });
      if (res.code == 200) {
        this.getList();
      }
    },

    // 发布
    async getRelease(id) {
      let res = await subFinancingRelease({ id: id });
      if (res.code == 200) {
        console.log(this.id);
        this.getList();
      }
    },

    async Del(companyId) {
      let res = await subFinancingDelete({ id: companyId });
      if (res.code == 200) {
        console.log(res);
      }
    },

    // 弹框
    Dele(id) {
      this.$confirm("此操作将永久删除该内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.Del(id);
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    choice(value) {
      this.radioChoice.forEach((el) => {
        if (el.value == value) {
          el.active = true;
          this.form.companySource = el.value;
        } else {
          el.active = false;
        }
      });
    },

    //发布
    release(id) {
      console.log(id);
      this.$confirm("此操作将提交申请内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.getRelease(id);
          this.getList();
          this.$message({
            type: "success",
            message: "发布成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消发布",
          });
        });
    },
    //关闭
    closeClick(companyFinancingId) {
      this.$confirm("此操作将永久关闭该内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.getClose(companyFinancingId);
          this.getList();
          this.$message({
            type: "success",
            message: "关闭成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },

    formatApplyStatus: function (financingStatusVal) {
      let ns = this;
      for (let i = 0; i < ns.applyStatusList.length; i++) {
        if (financingStatusVal == ns.applyStatusList[i].value) {
          return ns.applyStatusList[i];
        }
      }
      return { label: "" };
    },
    // 去详情
    toDetail(row) {
      this.$router.push({
        path: "myFinancingDetail",
        query: { id: row.companyFinancingId, status: row.status },
      });
    },

    // 去编辑
    toEdit(row) {
      this.$router.push({
        path: "myFinancingEdit",
        query: {
          id: row.companyFinancingId,
          status: row.status,
        },
      });
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
  },
};
</script>
<style scoped>
::v-deep .el-date-editor {
  width: 240px;
}
.breadcrumb {
  width: 1000px;
  margin: 20px 0;
}

.roomDiv {
  display: flex;
  justify-content: center;
}

.roomDiv .leftDiv {
  width: 200px;
  text-align: left;
}

.roomDiv .rightDiv {
  width: 800px;
  margin-left: 30px;
}
.titleDiv {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-align: left;
  padding: 10px;
  font-size: 20px;
  font-weight: bold;
  color: #045db9;
  margin-bottom: 20px;
  right: -5px;
  top: 0;
}
.titleDiv .sendBtn {
  font-weight: normal;
  color: #ffffff;
  background: #045db9;
  border-radius: 5px;
  font-size: 15px;
  display: flex;
  align-items: center;
  padding: 10px;
  cursor: pointer;
}
.layout {
  min-height: 100vh;
  position: relative;
  padding-bottom: 150px;
  box-sizing: border-box;
}

.msgDiv {
  margin: 40px 0;
}

.block {
  margin-top: 14px;
  text-align: right;
}
.rowBtn {
  cursor: pointer;
  margin-right: 12px;
}
::v-deep .el-dialog__header {
  text-align: left;
  border-bottom: 1px solid #000;
}
::v-deep .el-dialog--center .el-dialog__body {
  display: flex;
  justify-content: center;
}
.circleBoxRow {
  display: flex;
  align-items: center;
  margin-left: 20px;
}
.circleBox {
  display: flex;
  align-items: center;
  margin-right: 40px;
}
.circleBox .circle {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
  background: #ffffff;
  border: 3px solid #ffffff;
  box-shadow: 0 0 0 1px #dcdfe6;
  border-radius: 50%;
  margin-right: 8px;
}
.circleBox .active {
  background: #2997ff;
  box-shadow: 0 0 0 1px #2997ff;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
}
</style>